package tech.swh.ssm.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONObjectCodec;

import tech.swh.ssm.entity.App;
import tech.swh.ssm.entity.Category;
import tech.swh.ssm.service.AppService;
import tech.swh.ssm.service.CategoryService;
import tech.swh.ssm.service.DictionaryService;
import tech.swh.ssm.service.VersionService;

@Controller
@RequestMapping("/sys/backend")
public class BackController {

	@Autowired
	private AppService appservice;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private VersionService versionService;
	
	@RequestMapping(value = "goAppCheck.html",method = RequestMethod.GET)
	public String goAppCheck(Model model) {
		model.addAttribute("app_Version_List", appservice.getAppList(null, 0, 0, 0, 0,0,0));
		model.addAttribute("dictionaryList", dictionaryService.getDictionayList("APP_FLATFORM"));
		model.addAttribute("categoryList", categoryService.getCategoryList(0));
		return "back-applist";
	}
	
	@RequestMapping(value = "queryApp.action",method = RequestMethod.POST)
	public String goAppCheck(String appName,Integer dictionary,Integer onelevel,@RequestParam(value = "twolevel",required = false) Integer twolevel,@RequestParam(value = "threelevel",required = false) Integer threelevel,Model model) {
		if (twolevel==null)twolevel=0;
		if (threelevel==null)threelevel=0;
		model.addAttribute("app_Version_List",  appservice.getAppList(appName, dictionary, onelevel, twolevel, threelevel,0,0));
		model.addAttribute("dictionaryList", dictionaryService.getDictionayList("APP_FLATFORM"));
		model.addAttribute("categoryList", categoryService.getCategoryList(0));
		return "back-applist";
	}
	
	@ResponseBody
	@RequestMapping(value = "getCategory.ajax",method = RequestMethod.POST)
	public String getCategory(Integer parentid) {
		JSONObject result = new JSONObject();
		List<Category> catelist = categoryService.getCategoryList(parentid);
		result.put("catelist", catelist);
		return result.toJSONString();
	}
	
	@RequestMapping(value = "goAppStatus.html/{appId}",method = RequestMethod.GET)
	public String goAppStatus(@PathVariable Integer appId,Model model) {
		model.addAttribute("app_Version", appservice.getApp_Version(appId));
		model.addAttribute("dictionaryList", dictionaryService.getDictionayList("APP_FLATFORM"));
		model.addAttribute("categoryList", categoryService.getCategoryList(0));
		return "back-appStatus";
	}
	
	@ResponseBody
	@RequestMapping(value = "updateAppStatus.action",method = RequestMethod.POST)
	public String updateAppStatus(int appId,int status) {
		JSONObject result = new JSONObject();
		System.err.println(appId+"*"+status);
		int num = appservice.appUpdateStatus(appId, status);
		boolean flag= false;
		if (num>0) {
			flag = true;
			result.put("success", "操作成功");
			return result.toJSONString();
		}
		result.put("success", "操作失败");
		return result.toJSONString();
		
	}
	
}
